/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Buffer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 *
 * @author Toshiba
 */
public class FlightData {
    //Atributes
    private List<Flight> ListOfFlight = new ArrayList<Flight>();
    
    //Method
    private int getIndexOfFlight (String callSign)
    {
        int tempIdx = -1;
        
        //Iterate through elemens ListOfFlight
        for (int i = 0; i < ListOfFlight.size(); i++)
        {
            if (ListOfFlight.get(i).getCallSign().equals(callSign))
            {
                tempIdx = i;
            }
        }
        return (tempIdx);
    }

    private void RemoveFlight (String callSign)
    {
        for (int i = 0; i < ListOfFlight.size(); i++)
        {
            if (ListOfFlight.get(i).getCallSign().equals(callSign))
            {
                ListOfFlight.remove(i);
                System.out.println("removed : " + i);
            }
        }
    }
    
    public void addData (String code, String callSign, String country, float altitude, float groundspeed, float trackangle, float verticalrate, float latitude, float longitude, String timeOfDay) throws IOException
    {
        //Current time in seconds (from 1 Jan 1970)
        Calendar now = Calendar.getInstance();
        long lastUpdate = now.getTimeInMillis()/1000;

        Flight CFlight = new Flight(code, callSign, country, altitude, groundspeed, trackangle, verticalrate, latitude, longitude, timeOfDay, lastUpdate);
        int i = getIndexOfFlight(callSign);
        
        if (i != -1)
        {
            //Flight already exists in flight list
            ListOfFlight.remove(i);
            ListOfFlight.add(i, CFlight);
        }
        else
        {
            //Add new data
            ListOfFlight.add(CFlight);
        }
        
        System.out.println("Add flight data : ");
        System.out.println("Call sign : " + callSign);
        System.out.println("Latitude : " + latitude);
        System.out.println("Longitude : " + longitude);
        System.out.println("Last Update : " + lastUpdate);
        System.out.println("");

    }


    public List<Flight> getFlightList()
    {
        //Get all data in flightlist
        return (ListOfFlight);
    }

    public void OffCheck()
    {
        Calendar now;

        for (int i = 0; i < ListOfFlight.size(); i++)
        {
            now = Calendar.getInstance();
            long elapsedTime = now.getTimeInMillis()/1000 - ListOfFlight.get(i).getLastUpdate();

            //If elapsed time more than 1 minutes, then remove the flight data from list
            if (elapsedTime > 60)
            {
                System.out.println("Time Now : " + now.getTimeInMillis()/(1000));
                System.out.println("Remove : " + ListOfFlight.get(i).getCallSign() + "\n" );
                RemoveFlight(ListOfFlight.get(i).getCallSign());
            }
        }
        
    }
}
